package boss.portal.controller;

import boss.portal.entity.User;
import boss.portal.util.RSaUtil;
import com.nimbusds.jose.JOSEException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 描述：
 * <p>
 *
 * @author: zyh
 * @date: 2021/3/25 13:49
 */
@RestController
@Api(value = "登录管理", description = "登录管理")
public class LoginController extends BaseController {

    @ApiOperation(value = "自定义登录")
    @RequestMapping(value = "/login", method = {RequestMethod.GET})
    public void login(String username, String password, HttpServletResponse response) {
        User userVo = userRepository.findByUsername(username);
        if (userVo != null) {
            /*自定义生成Token，因为使用了自定义登录，就不会执行JWTLoginFilter了，所以需要字段调用生成token并返给前端*/
            // 这里可以根据用户信息查询对应的角色信息，这里为了简单，我直接设置个空list
            List roleList = new ArrayList<>();

            String subject = userVo.getUsername() + "-" + roleList;
                    String token = RSaUtil.buildToken(username, "http://52.130.194.37",  subject);
            // 登录成功后，返回token到header里面
             response.addHeader("Authorization", token);
        }
    }

    @GetMapping("/.well-known/jwks.json")
    @ResponseBody
    public Map<String, Object> getKey() throws JOSEException {
       return RSaUtil.getKeyJson();
    }
}

